package com.gupao.controller;

import org.apache.http.Header;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.gupao.model.ResultMap;
import com.gupao.model.ThirdToken;

@RestController
@RequestMapping("/unauthorized")
public class NoAuthController {
	private static final Logger log = LoggerFactory.getLogger(NoAuthController.class);

	private ResultMap resultMap = new ResultMap();

	@GetMapping(value = "/denied")
	public String denied() {
		log.info("小伙子权限不足,别无谓挣扎了...");
		return "denied...";
	}

	@GetMapping(value = "{message}")
	public ResultMap message(@PathVariable String message) {
		log.info("小伙子权限不足,别无谓挣扎了...");
		return resultMap.fail().code(401).message(message);
	}

	@GetMapping(value = "/get/third/user")
	public ResultMap getThirdUser(@RequestParam("code") String code) throws Exception {
		log.info("获取第三方用户信息。。。");
		// 创建一个客户端
		HttpClient client = new DefaultHttpClient();

		// 创建一个get方法
		HttpGet get = new HttpGet("http://localhost:8080/oauth/token?" + "grant_type=authorization_code" + "&code="
				+ code + "&client_id=tencent"
				+ "&client_secret=111111&redirect_uri=http://localhost:8080/unauthorized/get/third/user");

		// 执行请求
		HttpResponse res = client.execute(get);

		ThirdToken token = new ThirdToken();
		// 获取返回内容
		if (res.getEntity() != null) {
			JSONObject jsonObject = JSON.parseObject(EntityUtils.toString(res.getEntity()));
			token.setAccess_token(jsonObject.getString("access_token"));
			token.setRefresh_token(jsonObject.getString("refresh_token"));
			token.setScope(jsonObject.getString("scope"));
			token.setToken_type(jsonObject.getString("token_type"));
			token.setExpires_in(jsonObject.getString("expires_in"));
			System.out.println(token.getAccess_token());
		}

		// 关闭流
		EntityUtils.consume(res.getEntity());

		// 创建一个get方法
		HttpGet get1 = new HttpGet("http://localhost:8080/openapi/product/1?access_token=" + token.getAccess_token());
		// 执行请求
		HttpResponse res1 = client.execute(get1);
		String message = EntityUtils.toString(res1.getEntity());
		// 关闭流
		EntityUtils.consume(res1.getEntity());

		// 关闭连接
		client.getConnectionManager().shutdown();
		return resultMap.success().code(200).message(message);
	}

	@GetMapping("/order/{id}")
	public ModelAndView getOrder(@PathVariable String id) throws Exception {
		// //创建一个客户端
		// HttpClient client = new DefaultHttpClient();
		//
		// //创建一个get方法
		// HttpGet get = new HttpGet("http://localhost:8080/oauth/token?" +
		// "grant_type=authorization_code" +
		// "&code="+code+"&client_id=tencent" +
		// "&client_secret=111111&redirect_uri=http://localhost:8080/openapi/order/1");
		//
		// //执行请求
		// HttpResponse res = client.execute(get);
		//
		// //获取协议版本・・・「HTTP/1.1」
		// System.out.println(res.getProtocolVersion());
		// //获取返回状态码・・・「200」
		// System.out.println(res.getStatusLine().getStatusCode());
		// //获取原因短语・・・「OK」
		// System.out.println(res.getStatusLine().getReasonPhrase());
		// //获取完整的StatusLine・・・「HTTP/1.1 200 OK」
		// System.out.println(res.getStatusLine().toString());
		//
		// //获取返回头部信息
		// Header[] headers = res.getAllHeaders();
		// for (Header header : headers) {
		// System.out.println(header.getName() + ": " + header.getValue());
		// }
		//
		// //获取返回内容
		// if (res.getEntity() != null) {
		// System.out.println(EntityUtils.toString(res.getEntity()));
		// }
		//
		// //关闭流
		// EntityUtils.consume(res.getEntity());
		//
		// //关闭连接
		// client.getConnectionManager().shutdown();
		// return "Order responses:"+EntityUtils.toString(res.getEntity());
		System.out.println("go to the home page!");
		ModelAndView mode = new ModelAndView();
		mode.addObject("name", id);
		mode.setViewName("index");
		return mode;
	}

}
